package android.pot.com.smartcar.guidepage.activity;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.base.BaseFragment;
import android.pot.com.smartcar.guidepage.fragment.FirstFragment;
import android.pot.com.smartcar.guidepage.fragment.FourFragment;
import android.pot.com.smartcar.guidepage.fragment.LastFragment;
import android.pot.com.smartcar.guidepage.fragment.SecondFragment;
import android.pot.com.smartcar.guidepage.fragment.ThridFragment;
import android.pot.com.smartcar.guidepage.view.CicleView;
import android.pot.com.smartcar.ui.StartupPageActivity;
import android.pot.com.smartcar.util.StartupUtils;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class GuidePageActivity extends FragmentActivity {

	private List<BaseFragment> fragmentList;

	private TextView tv_tip,tv_top_tip;

	private ViewPager guidePage;

	private int curPos = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide_main);
		setImgTransparent(this);
		initData();
		initContentView();

	}
	public static void setImgTransparent(Activity activity){
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			Window window = activity.getWindow();
			window.clearFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
							| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			window.getDecorView().setSystemUiVisibility(
					View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  //不隐藏和透明虚拟导航栏  因为会遮盖底部的布局
							| View.SYSTEM_UI_FLAG_LAYOUT_STABLE//保持布局状态
			);
			window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
			window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);//不隐藏和透明虚拟导航栏  因为会遮盖底部的布局

		}
	}

	private void initData() {

		tv_tip = (TextView) findViewById(R.id.tv_tip);
		tv_top_tip = (TextView) findViewById(R.id.tv_top_tip);
		tv_top_tip.setVisibility(View.VISIBLE);
		tv_tip.setVisibility(View.VISIBLE);
		Typeface typeface = Typeface.createFromAsset(getAssets(), "font/HYDianYaTiJ.ttf");
		tv_tip.setTypeface(typeface);
		tv_top_tip.setTypeface(typeface);

//		BaseFragment fg0 = new android.pot.com.shifuhui.guidepage.fragment.InitFragment();
		BaseFragment fg1 = new FirstFragment();
		BaseFragment fg2 = new SecondFragment();
		BaseFragment fg3 = new ThridFragment();
		BaseFragment fg4 = new FourFragment();
		BaseFragment fg5 = new LastFragment();

		fragmentList = new ArrayList<BaseFragment>();
//		fragmentList.add(fg0);
		fragmentList.add(fg1);
		fragmentList.add(fg2);
		fragmentList.add(fg3);
		fragmentList.add(fg4);
		fragmentList.add(fg5);


	}

	private void initContentView() {
		guidePage = (ViewPager) findViewById(R.id.guide_page);
		final CicleView cicleView = (CicleView) findViewById(R.id.guide_cicle);
		cicleView.setCicleNum(fragmentList.size());

		guidePage.setAdapter(new GuidePageAdapter(getSupportFragmentManager(),
				fragmentList, this));
		guidePage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (curPos == 4){
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //最后设置，防止在引导页退出，下次登录出现的问题。
                            StartupUtils.setFirstStartUp(GuidePageActivity.this);
                            Intent intent=new Intent(GuidePageActivity.this, StartupPageActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }, 1000);

					return;
				}
			}
		});
		guidePage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int pos) {
				curPos = pos;
				if (pos == 4){
//					cicleView.setVisibility(View.INVISIBLE);
				}else{
					cicleView.setVisibility(View.VISIBLE);
				}
				tv_tip.setVisibility(View.VISIBLE);
				if (pos == 0){
					tv_top_tip.setText("智慧养车");
					tv_top_tip.setTextColor(Color.parseColor("#df191a"));
					tv_tip.setText("引领消费5.0时代");
				}
				else if (pos == 1){
					tv_top_tip.setText("智慧养车商城系统平台");
					tv_top_tip.setTextColor(Color.parseColor("#dc8810"));
					tv_tip.setText("购物领红包 • 购物赠福惠积分");
				}else if(pos == 2){
					tv_top_tip.setText("智慧养车质量监督检查专员");
					tv_top_tip.setTextColor(Color.parseColor("#146d1d"));
					tv_tip.setText("消费省钱 分享赚钱 质量监督 正品保障");
				}else if(pos == 3){
					tv_top_tip.setText("一分钱当两分花");
					tv_top_tip.setTextColor(Color.parseColor("#55acfe"));
					tv_tip.setText("积分转化系统让您的消费更实惠");
				}else if(pos == 4){
					tv_top_tip.setText("消费=养老");
					tv_top_tip.setTextColor(Color.parseColor("#e25911"));
					tv_tip.setText("积分有用于兑换人寿保险金");
				}

				cicleView.setCicleSolid(pos);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int pos) {
			}
		});
		guidePage.setCurrentItem(0);
	}

}
